package com.example.pungolo.journal;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class SettingsActivity extends Activity {
  public final static String[] fontPaths = {
		null,
		"fonts/Athena_Unicode.ttf",
		"fonts/CAC_Champagne.ttf",
		"fonts/Futura-Book.ttf",
		"fonts/Gabrielle.ttf",
		"fonts/Jacoba_Bold.ttf"
	 };
	public final static String[] fontNames = {
	 	"Default",
		"Athena",
		"Champagne",
		"Futura-Book",
		"Gabrielle",
		"Jacoba"
	 };

  public final static String GRACE_TIME = "tempoSalvato";
  public final static String FONT_TYPE = "fontSalvato";
  public final static String FONT_SIZE = "dimSalvata";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		
		View v;
		final SharedPreferences s = getSharedPreferences("settings", 0);
		
		
		v = findViewById(R.id.testoInfo1);
		final TextView testoInfo1 = (TextView) v;
		
		testoInfo1.setText("Tempo di modifica:");
		
		v = findViewById(R.id.barraTempo);
		final SeekBar barraTempo = (SeekBar)v;
		
		v = findViewById(R.id.testoTempo);
		final TextView testoTempo = (TextView) v;
		
		barraTempo.setProgress(s.getInt(GRACE_TIME, 5) - 5);
		
		testoTempo.setText(String.valueOf(5 + barraTempo.getProgress()) + " minuti");
		
		barraTempo.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			public void onStopTrackingTouch(SeekBar seekBar) {
				int tempoSalvato = 5 + barraTempo.getProgress();
				Editor e = s.edit();
				e.putInt(GRACE_TIME, tempoSalvato);
				e.commit();
			}
			
			public void onStartTrackingTouch(SeekBar seekBar) { }
			
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				testoTempo.setText(String.valueOf(5 + progress) + " minuti");
			}
		});
		
		
		
		v = findViewById(R.id.testoInfo2);
		final TextView testoInfo2 = (TextView) v;
		
		testoInfo2.setText("Seleziona il font:");
		
		v = findViewById(R.id.testoInfo3);
		final TextView testoInfo3 = (TextView) v;
		
		testoInfo3.setText("Seleziona la dimensione del font:");
		
		
		v = findViewById(R.id.listaFont);
		
		final Spinner listaFont = (Spinner) v;
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
				android.R.layout.simple_spinner_item,
				fontNames );
        listaFont.setAdapter(adapter);
		
        String salvato = s.getString(FONT_TYPE, "arial" );
        int pos = 0;
        
        for(int i = 1; i < fontPaths.length; i++)
        {
        	if (fontPaths[i].equals(salvato))
        		pos = i;	
        }
        
        listaFont.setSelection(pos);
        
        listaFont.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				String fontSalvato = fontPaths[position];
				Editor e = s.edit();
			
				e.putString(FONT_TYPE, fontSalvato);
				e.commit();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) { }
        });

        
        final Integer[] dimNames = new Integer[25];
	 
   	    for(int i = 0; i < dimNames.length; i++ )
   	    {
   		   int k = i + 8;
   		   dimNames[i] = k ;
   		 
   	    }
        
		v = findViewById(R.id.dimFont);
		
		final Spinner dimFont = (Spinner) v;
		
		ArrayAdapter<Integer> adapterDim = new ArrayAdapter<Integer>(this, 
				android.R.layout.simple_spinner_item,
				dimNames );
        dimFont.setAdapter(adapterDim);
		
        int dimSalvata = s.getInt(FONT_SIZE, 8);
        int pos2 = 0;
        
        pos2 = dimSalvata - 8;
        dimFont.setSelection(pos2);
        
        dimFont.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				int dimSalvata = dimNames[position];
				Editor e = s.edit();
			
				e.putInt(FONT_SIZE, dimSalvata);
				e.commit();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) { }
        });

	
	}

}
